package com.zhanghe.im;

import com.zhanghe.im.decoder.PacketDecoder;
import com.zhanghe.im.encoder.PacketEncoder;
import com.zhanghe.im.handler.*;
import com.zhanghe.im.statehandler.IMIdleStateHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

public class NettyServer {
    public static void main(String[] args) {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap
                .group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
//                        ch.pipeline().addLast(new FirstServerHandler());
//                        ch.pipeline().addLast(new ServerHandler());
//                        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,7,4));
                        //空闲检测
                        ch.pipeline().addLast(new IMIdleStateHandler());
                        ch.pipeline().addLast(new LifeCyCleTestHandler());
                        ch.pipeline().addLast(new Spliter());
                        ch.pipeline().addLast(new PacketDecoder());
                        ch.pipeline().addLast(new SimpleLoginRequestHandler());
                        ch.pipeline().addLast(new SimpleHeartBeatRequestHandler());
                        ch.pipeline().addLast(new AuthHandler());
                        ch.pipeline().addLast(new SimpleMessageRequestHandler());
                        ch.pipeline().addLast(new SimpleCreateGroupRequestHandler());
                        ch.pipeline().addLast(new SimpleJoinGroupRequestHandler());
                        ch.pipeline().addLast(new SimpleQuitGroupRequestHandler());
                        ch.pipeline().addLast(new SimpleListGroupRequestHandler());
                        ch.pipeline().addLast(new PacketEncoder());
                    }
                });
        bind(serverBootstrap,1000);
    }

    private static void bind(final ServerBootstrap serverBootstrap,final int port){
        serverBootstrap.bind(port).addListener(new GenericFutureListener<Future<? super Void>>() {
            @Override
            public void operationComplete(Future<? super Void> future) throws Exception {
                if(future.isSuccess()){
                    System.out.println("端口["+port+"]绑定成功!");
                }else{
                    System.out.println("端口["+port+"]绑定失败!");
                    bind(serverBootstrap,port+1);
                }
            }
        });
    }
}
